package com.yupi.exception;

import com.yupi.common.BaseResponse;
import com.yupi.common.ErrorCode;
import com.yupi.common.ResultUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.builder.BuilderException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author 木兔光太郎
 */
@RestControllerAdvice
@Slf4j
public class GlobalException {

    //只去捕获该异常
    @ExceptionHandler(BusinessException.class)
    public BaseResponse businessHandler(BusinessException e) {
        log.error("businessHandler,{}", e.getMessage());
        return ResultUtils.fail(e.getCode(), e.getMessage(), e.getDescription());

    }

    //系统内部异常
    @ExceptionHandler(RuntimeException.class)
    public BaseResponse runtimeHandler(RuntimeException e) {
        log.error("runTimeException,{}", e.getMessage());
        return ResultUtils.fail(ErrorCode.SYSTEM_ERROR, e.getMessage(), "");
    }
}
